package com.konsung.ksdc.manager.community;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.common.utils.DensityUtils;
import com.konsung.ksdc.common.utils.EmojiUtils;

/**
 * 创建者     kgh
 * 创建时间   2016/5/27 15:28
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class EmojiGridAdapter extends BaseAdapter {
    private Context mContext;

    /**
     * 构造方法
     * @param context 上下文
     */
    public EmojiGridAdapter(Context context) {
        this.mContext = context;

        if (EmojiUtils.emojiMap != null && EmojiUtils.emojiMap.size() != 0) {
            EmojiUtils.init();
        }
    }

    @Override
    public int getCount() {
        return EmojiUtils.emojiKeys.size();
    }

    @Override
    public Object getItem(int i) {
        return EmojiUtils.emojiKeys.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        ImageView ivEmoji;
        if (convertView == null) {
            ivEmoji = new ImageView(mContext);
            int width = DensityUtils.dp2px(mContext, 30);
            int height = DensityUtils.dp2px(mContext, 30);
            ivEmoji.setLayoutParams(new GridView.LayoutParams(width, height));
        } else {
            ivEmoji = (ImageView) convertView;
        }

        String key = EmojiUtils.emojiKeys.get(position);
        String pictureName = EmojiUtils.emojiMap.get(key);
        int resId = mContext.getResources().getIdentifier(pictureName, "drawable", mContext
                .getPackageName());
        ivEmoji.setImageResource(resId); //为ImageView设置图片资源
        ivEmoji.setBackgroundResource(R.drawable.me_ll_selector);

        return ivEmoji;
    }
}
